<?php

namespace app\admin\controller;

use think\Controller;
use think\Db;
use app\admin\model\Node as NodeModel;
use think\Cache;


class Office extends Admin
{

    //使用redis加载页面，
    public function supplies(){

        $res = Cache::get('name');
        if (empty($res)){
            $redis = new \Redis();
            $res= db('office_supplies')->select();
            $redis->connect('127.0.0.1', 6379);
            Cache::store('redis')->set('name',$res);
        }
        $page = db('office_supplies')->paginate(4);
       
        return view('admin@office/index',
              [
              'page'=>$page,
             'list' => $res
              ]);

      }

    //获取一条数据，放入页面
    public function getOffice($id)
    {
         $list= db('office_supplies')->where('id',$id)->find();
//         var_dump($list);exit;
        return view('admin@office/office_get',
            [
                'list' => $list
            ]);
    }
    //更新一条数据
    public function updateOffice($id)
    {   
        $data = $_POST;

        if(empty($data['supplies_name'])){
        	$this->error('请添加办公用品名称','admin/office/addOffice');
        }
         $data['supplies_name']=htmlentities($data['supplies_name']);
        if(empty($data['supplier'])){
        	$this->error('请添加供应商','admin/office/addOffice');
        }
        $data['supplier']=htmlentities($data['supplier']);
        if(!is_numeric($data['number'])){
        	$this->error('请输入正确格式','admin/office/addOffice');
        }
        $data['number']=htmlentities($data['number']);

        if(empty($data['unit_price'])){
        	$this->error('请输入单价','admin/office/addOffice');
        }
        $data['unit_price']=htmlentities($data['unit_price']);
        if(empty($data['total_price'])){
        	$this->error('总价参数错误','admin/office/addOffice');
        }
         $data['total_price'] =htmlentities($data['total_price']);
        if(empty($data['inventory'])){
        	$data['inventory']=$data['number'];
        }
        $result = db('office_supplies')->where('id',$id)->update($data);  

        if($result)
        {
            Cache::rm('name');
            $info['status']=true;
            $info['info']= '更新一条数据成功';
        }else{
            $info['status']=false;
            $info['info']= '更新失败,请填写更新数据';
        }
        return  json($info);
    }
    //用品出库
    public function upteOffice($id)
    {
			Cache::rm('name');
            $data = input('post.');
            $result = db('office_record')->insertGetId($data);
       if($result>0){
            $resl=$data['number'];
            $number = db('office_supplies')->where('id',$id)->find();
            $inventory = $number['inventory'];
            $res['inventory'] = $inventory - $resl;
            
            if($res['inventory']>=0){
              $inid = db('office_supplies')->where('id',$id)->update($res);
               $info['status']=true;
            }else{
            	$this->error('库存数量不足','admin/office/supplies');
            	 $info['status']=false;
            	 $info['msg']='库存数量不足';
            }          
        }
        return json($info);
    }
    //加载添加页面
    public function addOffice()
    {
        return  view('admin@office/form');
    }
    //添加一条数据到数据库。
    public function suppliesData()
    {
        $data = $_POST;
        if(empty($data['supplies_name'])){
        	$this->error('请添加办公用品名称','admin/office/addOffice');
        }
         $data['supplies_name']=htmlentities($data['supplies_name']);
        if(empty($data['supplier'])){
        	$this->error('请添加供应商','admin/office/addOffice');
        }
        $data['supplier']=htmlentities($data['supplier']);
        if(!is_numeric($data['number'])){
        	$this->error('请输入正确格式','admin/office/addOffice');
        }
        $data['number']=htmlentities($data['number']);

        if(empty($data['unit_price'])){
        	$this->error('请输入单价','admin/office/addOffice');
        }
        $data['unit_price']=htmlentities($data['unit_price']);
        if(empty($data['total_price'])){
        	$this->error('总价参数错误','admin/office/addOffice');
        }
         $data['total_price'] =htmlentities($data['total_price']);
        if(empty($data['inventory'])){
        	$data['inventory']=$data['number'];
        }       
       
        $officeid = db('office_supplies')->insert($data);

        if ($officeid){
            Cache::rm('name');
            $this->success('办公用品添加成功','admin/Office/supplies');
        }else{
            $this->error('办公用品添加失败','admin@office/suppliesData');
        }
    }
     //查询一条数据
    public function outbound($id)
    {

        $list= db('office_supplies')->where('id',$id)->find();

        return view('admin@office/outbound',
            [
                'list' => $list
            ]);
    }
    //删除一条数据
    public function delectOffice($id)
    {
        $result = db('office_supplies')->where(['id'=>$id])->delete();
        Cache::rm('name');
        if ($result) {
            $info['status'] = true;
        }else{
            $info['status'] = false;
        }
        return  json($info);
    }

    public function suppliesrecord()
    {
    	    Cache::rm('name');
    	    $list= db('office_record')->paginate(8);
			$page = $list->render();
			// 模板变量赋值
			$this->assign('list', $list);
			$this->assign('page', $page);
    	  return view('admin@office/index_record',
              [
	             'page'=>$page,
	             'list' => $list
              ]);
    }

    public function delectRecord($id)
    {
    	$result = db('office_record')->where(['id'=>$id])->delete();

        Cache::rm('name');
        
        if ($result) {
            $info['status'] = true;
        }else{
            $info['status'] = false;
        }
        return  json($info);
    }

}
